﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BookExchange.ODataService;
using BookExchange.CustomClass.Base;
using System.Linq.Expressions;
namespace BookExchange.Business
{
    public class RequestBusiness : BaseBusiness<Request>
    {
        public IQueryable<Request> List(System.Linq.Expressions.Expression<Func<ODataService.Request, bool>> predicate = null)
        {
            return base.GetQuery(DB.Requests, 0, 0, predicate);
        }

        public override void Insert(ODataService.Request obj)
        {
            DB.AddToRequests(obj);
            DB.SaveChanges();
        }

        public Request GetRequestByCode(int requestCode)
        {
            var request = DB.Requests.Where(r => r.RequestCode == requestCode).FirstOrDefault();

            return request;
        }

        public List<Request> GetRequestByCategory(int categoryCode)
        {
            var requestList = DB.Requests.Where(r => r.CategoryCode == categoryCode).ToList();

            return requestList;
        }

        public List<Request> GetRequestByCategoryAndRequestType(int categoryCode, string requestType)
        {
            var requestList = DB.Requests.Where(r => r.CategoryCode == categoryCode && r.RequestType == requestType).ToList();

            return requestList;
        }

        public List<ODataService.Request> GetTopRequest(string requestType, int take = 0)
        {
            var query = from r in DB.Requests
                        where r.RequestType == requestType
                           && r.Finished == 0
                           && r.Active == 1
                        orderby r.CreatingDate descending
                        select r;

            List<ODataService.Request> result = new List<ODataService.Request>();
            if (take > 0)
            {
                result = query.Take(take).ToList();
            }
            else
            {
                result = query.ToList();
            }

            return result;
        }

        public int CountActiveRequest(string requestType)
        {
            int result = 0;

            if (string.IsNullOrEmpty(requestType))
            {
                return result;
            }

            var query = from r in DB.Requests
                        where r.Finished == 0 &&
                              r.RequestType == requestType &&
                              r.Active == 1
                        select r;

            return query.Count();
        }

        public IQueryable<ODataService.Request> ListActiveRequest(string requestType, int skip = 1, int take = 20,
    System.Linq.Expressions.Expression<Func<ODataService.Request, bool>> predicate = null)
        {
            var query = DB.Requests.Where(r => r.Active == 1 && r.Finished == 0 && r.RequestType == requestType);
            query = base.GetQuery(query, skip, take, predicate);
            return query;
        }

        public IQueryable<ODataService.Request> GetQueryDefault()
        {
           return DB.Requests.Select(q => q);
        }
         
        public IQueryable<ODataService.Request> GetQueryFilterRequestType(IQueryable<ODataService.Request> query, string requestType )
        {
            if (query != null)
                return query.Where(r => r.RequestType.Equals(requestType));
            return query;
        }


        public IQueryable<ODataService.Request> GetQueryFilterUser(IQueryable<ODataService.Request> query, int memberCode )
        {
            if (query != null)
                return query.Where(r => r.MemberCode == memberCode);
            return query; 
        }

        public IQueryable<ODataService.Request> GetQueryFilterName(IQueryable<ODataService.Request> query, string name )
        {
            if (query != null)
                return query.Where( r => r.BookName.Equals(name, StringComparison.InvariantCultureIgnoreCase));
            return query;  
        }

        public IQueryable<ODataService.Request> GetQueryFilterCategory(IQueryable<ODataService.Request> query, int category )
        {
            if (query != null)
                return query.Where( r => r.CategoryCode == category);
            return query;   
        }

        public List<Request> GetListActiveRequest(IQueryable<ODataService.Request> query, int skip = 1, int take = 20)
        {
            List<Request> lst = new List<Request>();
            if (query != null)
            { 
                lst = query.Skip(skip).Take(take).ToList(); 
            }
            return lst;  
        }

        public int GetCountActiveRequest(IQueryable<ODataService.Request> query )
        {
            if(query != null)
              return query.Count();
            return 0;
        } 

        //    public IQueryable<ODataService.Request> ListActiveRequest(
        //        System.Linq.Expressions.Expression<Func<ODataService.Request,bool>> predicate = null)
        //    {
        //        return ListActiveRequest(RequestType.Request, predicate);
        //    }

        //    public IQueryable<ODataService.Request> ListActivePost(
        //System.Linq.Expressions.Expression<Func<ODataService.Request, bool>> predicate = null)
        //    {
        //        return ListActiveRequest(RequestType.Post, predicate);
        //    }

        //private IQueryable<ODataService.Request> GetQueryRequest(
        //    IQueryable<ODataService.Request> query,
        //    System.Linq.Expressions.Expression<Func<ODataService.Request, bool>> predicate = null)
        //{
        //    if (predicate == null)
        //    {
        //        return query;
        //    }
        //    else
        //    {
        //        return query.Where(predicate);
        //    }

        //}
    }
}


